﻿using System.Windows.Forms;
using System.IO;

namespace ContactManager.Common
{
    class WordUtil
    {
        public static void exportWord(object[] oBookMarks, string[] sValues, string fname)
        {
            object missingValue = System.Reflection.Missing.Value;
            object fileName = System.Windows.Forms.Application.StartupPath + "\\template\\" + fname;
            if (System.IO.File.Exists(fileName as string))
            {
                object myTrue = false;
                Microsoft.Office.Interop.Word._Application oWord = new Microsoft.Office.Interop.Word.ApplicationClass();
                Microsoft.Office.Interop.Word._Document oDoc;
                oDoc = oWord.Documents.Open(ref fileName, ref missingValue, ref myTrue,
                    ref missingValue, ref missingValue, ref missingValue,
                    ref missingValue, ref missingValue, ref missingValue,
                    ref missingValue, ref missingValue, ref missingValue,
                    ref missingValue, ref missingValue, ref missingValue,
                    ref missingValue);

                //MessageBox.Show(oDoc.Bookmarks.Count.ToString()); //得到word中的所有书签个数

                //写入Word文档
                //object[] oBookMarks = { "出让方名称", "出让方通讯地址", "出让方邮政编码" };//Word中的书签
                //string[] sValues = { "测试出让方名称", "测试出让方通讯地址43号", "测试出让方邮政编码432123" };   //给书签的值
                for (int iTemp = 0; iTemp < oBookMarks.Length; iTemp++)
                {

                    Microsoft.Office.Interop.Word.Range tmpRng = oWord.ActiveDocument.Bookmarks.get_Item(ref oBookMarks[iTemp]).Range;
                    tmpRng.Text = sValues[iTemp].ToString(); //通过tmpRng.Text还可以得到标签处的数据,在这儿是设置它的数据
                    object oRng = tmpRng;
                    oDoc.Bookmarks.Add(oBookMarks[iTemp].ToString(), ref oRng);

                }

                object bSaveChange = true;

                SaveFileDialog saveFileDialog = new SaveFileDialog();
                saveFileDialog.Filter = "导出Word (*.doc)|*.doc";
                saveFileDialog.FilterIndex = 0;
                saveFileDialog.RestoreDirectory = true;
                saveFileDialog.CreatePrompt = true;
                saveFileDialog.Title = "导出文件保存路径";
                saveFileDialog.ShowDialog();
                string strName = saveFileDialog.FileName;

                object sFileName = strName;

                //把写好的合同另存为
                if (oDoc.SaveFormat == (int)Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatDocument)
                {
                    oDoc.SaveAs(ref sFileName, ref missingValue, ref myTrue,
                         ref missingValue, ref missingValue, ref missingValue,
                         ref missingValue, ref missingValue, ref missingValue,
                         ref missingValue, ref missingValue, ref missingValue,
                         ref missingValue, ref missingValue, ref missingValue,
                         ref missingValue);
                    MessageBox.Show("导出成功");
                }
                oDoc.Close(ref bSaveChange, ref missingValue, ref missingValue);
                oWord.Quit(ref bSaveChange, ref missingValue, ref missingValue);
            }
            else
            {
                MessageBox.Show("模板不存在！");
                return;
            }
            //object tmp = "msr_01";
            //Word.Range tmpRng = oWord.ActiveDocument.Bookmarks.get_Item(ref tmp).Range;
            //tmpRng.Text = "海口市国土局信息服务中心";
            ////由于附值以后书签自动消除，为了以后便于修改，需要把书签再自动生成一个
            //object oRng = tmpRng;
            //oDoc.Bookmarks.Add(tmp.ToString(), ref oRng);
            //object bSaveChange = true;
            //oDoc.Close(ref bSaveChange, ref missingValue, ref missingValue);
        }
    }
}
